package bookshop.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import bookshop.beans.Book;
import bookshop.service.BookService;

public class AddBookServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		BookService bookService=new BookService();
		request.setCharacterEncoding("utf-8");
		String title=request.getParameter("title");
		String add_num=request.getParameter("add_num");
		if(bookService.bookIsExist(title)) {
			bookService.addBookStore(title,Integer.parseInt(add_num));
			request.getRequestDispatcher("/bookmanage/addbook.jsp").forward(request, response);
			return;
		}
		String author=request.getParameter("author");
		double price=Double.parseDouble(request.getParameter("price"));
		String pdate=request.getParameter("pdate");
		Book book=new Book(author,title,price,pdate,0,Integer.parseInt(add_num),null);
		bookService.addBook(book);
		request.getRequestDispatcher("/bookmanage/addbook.jsp").forward(request, response);
	}

}
